package com.android.huimeiqupin.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.huimeiqupin.fragment.group.Fragment_TabRecommendList;
import com.android.huimeiqupin.model.MainTabData;
import com.android.huimeiqupin.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 热搜页
 */
public class Fragment2 extends Fragment {

    @BindView(R.id.tab_hot)
    TabLayout tabLayout;
    @BindView(R.id.pager_hot)
    ViewPager viewPager;
    @BindView(R.id.main_top_title)
    TextView mainTopTitle;
    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    private FragmentActivity mcontext;
    private View view;

    public Fragment2() {
    }

    private ArrayList<MainTabData> names = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_fragment2, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        initUI();
        return view;
    }

    private void initUI() {
        mainTopTitle.setText("热搜");
        mainTopTitle.setTextColor(Color.parseColor("#ffffff"));
        mainTopBg.setBackgroundColor(Color.parseColor("#FF2741"));
        names.add(new MainTabData("大家都在买", "&order=sales&sort=DESC"));
        names.add(new MainTabData("最新", "&order=new&sort=DESC"));
        HotFragmentPagerAdapter pagerAdapter = new HotFragmentPagerAdapter(mcontext.getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private class HotFragmentPagerAdapter extends FragmentPagerAdapter {
        private HotFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return Fragment_TabRecommendList.newInstance(names.get(position).getId());
        }

        @Override
        public int getCount() {
            return names.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return names.get(position).getName();
        }
    }
}